package com.iemes.controller.hr;

/**
 * 扣罚款
 */
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.controller.index.BaseController;
import com.iemes.entity.AttendancePunishFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.DockFormMap;
import com.iemes.entity.ElectricityFormMap;
import com.iemes.entity.Electricity_groupFormMap;
import com.iemes.entity.MealFormMap;
import com.iemes.mapper.hr.AttendanceFineMapper;
import com.iemes.mapper.hr.DepartmentinfoMapper;
import com.iemes.mapper.hr.ElectricityMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("/hrattendance/")
public class AttendanceFineController extends BaseController {
	@Inject
	private DepartmentinfoMapper departmentinfoMapper;
	@Inject
	private AttendanceFineMapper attendanceFineMapper;
	@Inject
	private ElectricityMapper electricityMapper;

	// 扣罚款页面
	@RequestMapping("attendanceAttend")
	public String examineOrder(Model model) {
		DepartmentForMap departmentForMap = getFormMap(DepartmentForMap.class);
		departmentForMap.put("where", "where id!=16 ");
		// 查部门名称
		List<DepartmentForMap> department = departmentinfoMapper
				.findByWhere(departmentForMap);
		model.addAttribute("caset", department);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/hr/attendance_list";
	}

	// 扣罚款分页
	@ResponseBody
	@RequestMapping("attendance_findByPage")
	public PageView findByPage(String pageNow, String pageSize)
			throws Exception {
		DockFormMap dockFormMap=getFormMap(DockFormMap.class);
		List<Map<String,Object>> dockFormMaps=attendanceFineMapper.getAllAttendance(dockFormMap);
        for(Map map:dockFormMaps){
        	//总扣罚金额
        	Double countMoney=0.0;
        	//总扣罚次数
        	Integer countNumber=0;
        	if(map.get("attend_id")!=null){//考勤
        		String attend_id=map.get("attend_id").toString();
        		AttendancePunishFormMap attendancePunishFormMap=attendanceFineMapper.findbyFrist("id", attend_id, AttendancePunishFormMap.class);
        		String attenMoneys= attendancePunishFormMap.get("money").toString();
        		Double attenMoney=Double.valueOf(attenMoneys);
        		countMoney+=attenMoney;
        		countNumber+=1;
        	} 
        	if(map.get("meal_id")!=null){//餐费
        		String meal_id=map.get("meal_id").toString();
        		MealFormMap mealFormMap=attendanceFineMapper.findbyFrist("id", meal_id, MealFormMap.class);
        		String mealMoneys= mealFormMap.get("money").toString();
        		Double mealMoney=Double.valueOf(mealMoneys);
        		countMoney+=mealMoney;
        		countNumber+=1;
        	} 
        	if(map.get("electricity_id")!=null){//电费
        		String electricity_id=map.get("electricity_id").toString();
        		ElectricityFormMap electricityFormMap=attendanceFineMapper.findbyFrist("id", electricity_id, ElectricityFormMap.class);
        		String group_id= electricityFormMap.get("group_id").toString();
        		//查询总人数
        		List<Electricity_groupFormMap> countmean= electricityMapper.getDormitory(group_id);
        		Integer countPerson=countmean.size();
        		String electricityMoneys= electricityFormMap.get("money").toString();
        		Double countelectricityMoneys=Double.valueOf(electricityMoneys);//宿舍总金额
        		Double countelectricityMoney=countelectricityMoneys/countPerson;
        		countMoney+=countelectricityMoney;
        		countNumber+=1;
        	}
        	
        	map.put("countMoney", countMoney);
        	map.put("countNumber", countNumber);
        }
		
		
		
		LuogicPaging(pageNow, pageSize,dockFormMaps );
		pageView.setRecords(dockFormMaps);
		return pageView;
	}

}
